<template>
  <div class="app-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-left">
        <el-button icon="Back" @click="goBack">返回</el-button>
                 <span class="page-title">已审核</span>
      </div>
      <div class="header-right">
        <span class="user-info">信息发布</span>
      </div>
    </div>

    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发布者" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入发布者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable style="width: 200px;">
          <el-option label="审核通过" :value="2" />
          <el-option label="审核不通过" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="purchaseList">
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="发布者" align="center" prop="publisher" />
      <el-table-column label="发布时间" align="center" prop="time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布内容" align="center" prop="content" />
             <el-table-column label="审核状态(已审核)" align="center" prop="status">
         <template #default="scope">
           <el-tag :type="scope.row.status === 2 ? 'success' : 'danger'">
             {{ scope.row.status === 2 ? '审核通过' : '审核不通过' }}
           </el-tag>
         </template>
       </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['system:purchase:query']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="PurchasePublished">
import { listPurchase } from "@/api/system/purchase"
import { useRouter } from 'vue-router'

const router = useRouter()
const { proxy } = getCurrentInstance()

const purchaseList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    publisher: null,
    status: null, // 审核通过或审核不通过
  }
})

const { queryParams } = toRefs(data)

/** 查询已发布列表 */
function getList() {
  loading.value = true
  // 查询状态为2(审核通过)或3(审核不通过)的数据
  const params = { ...queryParams.value }
  if (!params.status) {
    // 如果没有选择状态，则查询所有已审核的数据
    params.status = [2, 3]
  }
  listPurchase(params).then(response => {
    purchaseList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  queryParams.value.publisher = null
  queryParams.value.status = null
  handleQuery()
}

/** 详情按钮操作 */
function handleDetail(row) {
  // 跳转到详情页面
  proxy.$router.push(`/system/purchase/detail/${row.id}`)
}

/** 返回上一页 */
function goBack() {
  router.go(-1)
}

// 页面加载时获取数据
onMounted(() => {
  getList()
})
</script>

<style scoped>
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background: #fff;
  border-bottom: 1px solid #e4e7ed;
  margin-bottom: 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.page-title {
  font-size: 18px;
  font-weight: bold;
  color: #303133;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-info {
  color: #606266;
  font-size: 14px;
}
</style> 